package string;

import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;

/**
 * @Author: hujian
 * Date: 2018/12/14 18:30
 * Description: <描述>
 */
public class SubString {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        String version_list;
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "\n" +
                "<config-set>\n" +
                "  <policy-list>\n" +
                "    <policy>\n" +
                "      <CPID>08010002</CPID>\n" +
                "      <name>emshop.rcscdn.fetionpic.com</name>\n" +
                "      <host>emshop.rcscdn.fetionpic.com</host>\n" +
                "      <basic-conf>\n" +
                "        <url>\n" +
                "          <matches>1</matches>\n" +
                "          <type>1</type>\n" +
                "          <value><![CDATA[^http://emshop\\.rcscdn\\.fetionpic\\.com/(([^?]*)\\.(?:3gp|7z|apk|avi|bzip2|cab|chm|deb|eot|exe|gz|dat|mp3|mp4|flac|ts|aac|gzip|iso|jar|rar|tar|tgz|wma|zip|pdf|patch|ogg|swf|msi|bzip|tz|bz2|wav))]]></value>\n" +
                "        </url>\n" +
                "      </basic-conf>\n" +
                "      <pro-conf>\n" +
                "        <cache_ttl>10</cache_ttl>\n" +
                "        <cache_index><![CDATA[emshop.rcscdn.fetionpic.com/$1/$2]]></cache_index>\n" +
                "        <cache_always>yes</cache_always>\n" +
                "      </pro-conf>\n" +
                "    </policy>\n" +
                "  </policy-list>\n" +
                "</config-set>\n";
        version_list = subString(xml, "<version-list>", "</version-list>");
        System.out.println("截取后的字符串：\n" + version_list);
        if (version_list.equals("")){
            System.out.println("123456");
        }
        int cdn_big;
        int cdn_small;
        int webcache_big;
        int webcache_small;
        /*cdn_big = Integer.parseInt(subString(version_list, "<cdn_big>", "</cdn_big>"));
        cdn_small = Integer.parseInt(subString(version_list, "<cdn_small>", "</cdn_small>"));
        webcache_big = Integer.parseInt(subString(version_list, "<webcache_big>", "</webcache_big>"));
        webcache_small = Integer.parseInt(subString(version_list, "<webcache_small>", "</webcache_small>"));
        System.out.println(cdn_big + "," + cdn_small + "," + webcache_big + "," + webcache_small);*/
        String h_xml = xml.replaceAll(version_list, "");
        System.out.println("去掉version_list后的xml：)" + h_xml);

    }

    public static String subString(String str, String strStart, String strEnd) {

        /* 找出指定的2个字符在 该字符串里面的 位置 */
        int strStartIndex = str.indexOf(strStart);
        int strEndIndex = str.indexOf(strEnd);
        String result;
        /* index 为负数 即表示该字符串中 没有该字符 */
        if (strStartIndex < 0) {
            return "";
        }
        if (strEndIndex < 0) {
            return "";
        }
        /* 开始截取 */
        if ("<version-list>".equals(strStart)) {
             result = str.substring(strStartIndex, strEndIndex + 15);
        }else {
             result = str.substring(strStartIndex, strEndIndex).substring(strStart.length());
        }
        return result;
    }
}


